package com.lzy.mall.product.controller;


import cn.hutool.core.collection.CollUtil;
import com.lzy.mall.common.utils.PageUtils;
import com.lzy.mall.common.utils.ResultUtils;
import com.lzy.mall.product.entity.PmsProductAttrValue;
import com.lzy.mall.product.service.PmsAttrService;
import com.lzy.mall.product.service.PmsProductAttrValueService;
import com.lzy.mall.product.vo.AttrRespVo;
import com.lzy.mall.product.vo.PmsAttrVO;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;

/**
 * 商品属性(PmsAttr)表控制层
 *
 * @author lzy
 * @since 2021-08-25 11:41:06
 */
@RestController
@RequestMapping("attr")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class PmsAttrController {

    private final PmsAttrService attrService;

    private final PmsProductAttrValueService productAttrValueService;

    /**
     * 根据spuID查询产品属性值列表
     * 获取spu规格
     * @param spuId
     * @return
     */
    @GetMapping("/base/listforspu/{spuId}")
    public ResultUtils baseAttrlistforspu(@PathVariable("spuId") Long spuId){

        List<PmsProductAttrValue> entities = productAttrValueService.baseAttrlistforspu(spuId);

        return ResultUtils.ok().put("data",entities);
    }

    /**
     * 保存商品属性及其关联
     */
    @PostMapping("/save")
    //@RequiresPermissions("product:attr:save")
    public ResultUtils save(@RequestBody PmsAttrVO attrVO){
        attrService.saveAttr(attrVO);

        return ResultUtils.ok();
    }

    /**
     * 规格参数列表
     * @param params
     * @param catelogId
     * @param type
     * @return
     */
    @GetMapping("/{attrType}/list/{catelogId}")
    public ResultUtils baseAttrList(@RequestParam Map<String, Object> params,
                          @PathVariable("catelogId") Long catelogId,
                          @PathVariable("attrType")String type){

        PageUtils page = attrService.queryBaseAttrPage(params,catelogId,type);
        return ResultUtils.ok().put("page", page);
    }
    @GetMapping("/list")
    public ResultUtils baseAttrList(@RequestParam Map<String, Object> params){

        PageUtils page = attrService.queryBaseAttrPage(params,null,null);
        return ResultUtils.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    //@RequiresPermissions("product:attr:info")
    public ResultUtils info(@PathVariable("attrId") Long attrId){
        AttrRespVo respVo = attrService.getAttrInfo(attrId);
        //TODO attr --> data
        return ResultUtils.ok().put("data", respVo);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attr:update")
    public ResultUtils update(@RequestBody PmsAttrVO attr){
        attrService.updateAttr(attr);

        return ResultUtils.ok();
    }

    /**
     * 修改商品规格
     * @param spuId
     * @param
     * @return
     */
    @PostMapping("/update/{spuId}")
    public ResultUtils updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<PmsProductAttrValue> list){

        productAttrValueService.updateSpuAttr(spuId,list);

        return ResultUtils.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attr:delete")
    public ResultUtils delete(@RequestBody Long[] attrIds){
        attrService.removeByIds(CollUtil.toList(attrIds));

        return ResultUtils.ok();
    }
}
